/*
 * Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
 *
 * Licensed under the Mulan PSL v2.
 * You can use this file according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of the License at
 *
 *     http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
 * FOR MORE DETAILS.
 */

#pragma once

#include "highfive/H5File.hpp"

#include "DBManager/IRepository/Input/Design/IDRESP2Repository.h"
#include "DBManager/Repository/HDF5/Base/MainlyRepositoryBase.h"
SG_DBMANAGER_HDF5_NAMESPACE_OPEN
namespace DRESP2
{
    struct IDENTITY
    {
        SG::DataStructure::Common::Id   ID          = 0;      ///<
        char                            LABEL[8]    = { 0 };  ///<
        SG::DataStructure::Common::Id   EQID        = 0;      ///<
        SG::DataStructure::Common::I64  REGION      = 0;      ///<
        SG::DataStructure::Common::I64  METHOD      = 0;      ///<
        SG::DataStructure::Common::Real C1          = 0.0;    ///<
        SG::DataStructure::Common::Real C2          = 0.0;    ///<
        SG::DataStructure::Common::Real C3          = 0.0;    ///<
        SG::DataStructure::Common::I64  DESVAR_POS  = 0;      ///<
        SG::DataStructure::Common::I64  DTABLE_POS  = 0;      ///<
        SG::DataStructure::Common::I64  DRESP1_POS  = 0;      ///<
        SG::DataStructure::Common::I64  DNODE_POS   = 0;      ///<
        SG::DataStructure::Common::I64  DVPREL1_POS = 0;      ///<
        SG::DataStructure::Common::I64  DVCREL1_POS = 0;      ///<
        SG::DataStructure::Common::I64  DVMREL1_POS = 0;      ///<
        SG::DataStructure::Common::I64  DVPREL2_POS = 0;      ///<
        SG::DataStructure::Common::I64  DVCREL2_POS = 0;      ///<
        SG::DataStructure::Common::I64  DVMREL2_POS = 0;      ///<
        SG::DataStructure::Common::I64  DRESP2_POS  = 0;      ///<
        SG::DataStructure::Common::I64  DESVAR_LEN  = 0;      ///<
        SG::DataStructure::Common::I64  DTABLE_LEN  = 0;      ///<
        SG::DataStructure::Common::I64  DRESP1_LEN  = 0;      ///<
        SG::DataStructure::Common::I64  DNODE_LEN   = 0;      ///<
        SG::DataStructure::Common::I64  DVPREL1_LEN = 0;      ///<
        SG::DataStructure::Common::I64  DVCREL1_LEN = 0;      ///<
        SG::DataStructure::Common::I64  DVMREL1_LEN = 0;      ///<
        SG::DataStructure::Common::I64  DVPREL2_LEN = 0;      ///<
        SG::DataStructure::Common::I64  DVCREL2_LEN = 0;      ///<
        SG::DataStructure::Common::I64  DVMREL2_LEN = 0;      ///<
        SG::DataStructure::Common::I64  DRESP2_LEN  = 0;      ///<
        SG::DataStructure::Common::Id   DOMAIN_ID   = 0;      ///<
    };
};  // namespace DRESP2
SG_DBMANAGER_HDF5_NAMESPACE_CLOSE
template <>
inline HighFive::DataType HighFive::create_datatype<SG::DBManager::HDF5::DRESP2::IDENTITY> ()
{
    HighFive::CompoundType compoundType = { { "ID", HighFive::create_datatype<SG::DataStructure::Common::Id> () },
                                            { "LABEL", HighFive::create_datatype<char[8]> (), 1, true },
                                            { "EQID", HighFive::create_datatype<SG::DataStructure::Common::Id> () },
                                            { "REGION", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "METHOD", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "C1", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
                                            { "C2", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
                                            { "C3", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
                                            { "DESVAR_POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "DTABLE_POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "DRESP1_POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "DNODE_POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "DVPREL1_POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "DVCREL1_POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "DVMREL1_POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "DVPREL2_POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "DVCREL2_POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "DVMREL2_POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "DRESP2_POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "DESVAR_LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "DTABLE_LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "DRESP1_LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "DNODE_LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "DVPREL1_LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "DVCREL1_LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "DVMREL1_LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "DVPREL2_LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "DVCREL2_LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "DVMREL2_LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "DRESP2_LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "DOMAIN_ID", HighFive::create_datatype<SG::DataStructure::Common::Id> () } };
    return compoundType;
}

template <>
inline HighFive::DataType HighFive::create_datatype<SG::DataStructure::FEM::DRESP2Data::DESVARData> ()
{
    HighFive::CompoundType ct = { { "DVID", HighFive::create_datatype<SG::DataStructure::Common::Id> () } };
    return ct;
}
template <>
inline HighFive::DataType HighFive::create_datatype<SG::DataStructure::FEM::DRESP2Data::DNODEData> ()
{
    HighFive::CompoundType ct = { { "GRID", HighFive::create_datatype<SG::DataStructure::Common::Id> () },
                                  { "COMP", HighFive::create_datatype<SG::DataStructure::Common::I64> () } };
    return ct;
}
template <>
inline HighFive::DataType HighFive::create_datatype<SG::DataStructure::FEM::DRESP2Data::DRESP1Data> ()
{
    HighFive::CompoundType ct = { { "R1ID", HighFive::create_datatype<SG::DataStructure::Common::Id> () } };
    return ct;
}
template <>
inline HighFive::DataType HighFive::create_datatype<SG::DataStructure::FEM::DRESP2Data::DRESP2SubData> ()
{
    HighFive::CompoundType ct = { { "R2ID", HighFive::create_datatype<SG::DataStructure::Common::Id> () } };
    return ct;
}
template <>
inline HighFive::DataType HighFive::create_datatype<SG::DataStructure::FEM::DRESP2Data::DTABLEData> ()
{
    HighFive::CompoundType ct = { { "LABEL", HighFive::create_datatype<char[8]> (), 1, true } };
    return ct;
}
template <>
inline HighFive::DataType HighFive::create_datatype<SG::DataStructure::FEM::DRESP2Data::DVCREL1Data> ()
{
    HighFive::CompoundType ct = { { "DC1ID", HighFive::create_datatype<SG::DataStructure::Common::Id> () } };
    return ct;
}
template <>
inline HighFive::DataType HighFive::create_datatype<SG::DataStructure::FEM::DRESP2Data::DVCREL2Data> ()
{
    HighFive::CompoundType ct = { { "DC2ID", HighFive::create_datatype<SG::DataStructure::Common::Id> () } };
    return ct;
}
template <>
inline HighFive::DataType HighFive::create_datatype<SG::DataStructure::FEM::DRESP2Data::DVMREL1Data> ()
{
    HighFive::CompoundType ct = { { "DM1ID", HighFive::create_datatype<SG::DataStructure::Common::Id> () } };
    return ct;
}
template <>
inline HighFive::DataType HighFive::create_datatype<SG::DataStructure::FEM::DRESP2Data::DVMREL2Data> ()
{
    HighFive::CompoundType ct = { { "DM2ID", HighFive::create_datatype<SG::DataStructure::Common::Id> () } };
    return ct;
}
template <>
inline HighFive::DataType HighFive::create_datatype<SG::DataStructure::FEM::DRESP2Data::DVPREL1Data> ()
{
    HighFive::CompoundType ct = { { "DP1ID", HighFive::create_datatype<SG::DataStructure::Common::Id> () } };
    return ct;
}
template <>
inline HighFive::DataType HighFive::create_datatype<SG::DataStructure::FEM::DRESP2Data::DVPREL2Data> ()
{
    HighFive::CompoundType ct = { { "DP2ID", HighFive::create_datatype<SG::DataStructure::Common::Id> () } };
    return ct;
}
SG_DBMANAGER_HDF5_NAMESPACE_OPEN
class DRESP2Repository : public IDRESP2Repository, public SG::DBManager::HDF5::MainlyRepositoryBase<SG::DataStructure::FEM::DRESP2Data>
{
    using MainStruct       = SG::DataStructure::FEM::DRESP2Data;
    using SubStructDESVAR  = SG::DataStructure::FEM::DRESP2Data::DESVARData;
    using SubStructDNODE   = SG::DataStructure::FEM::DRESP2Data::DNODEData;
    using SubStructDRESP1  = SG::DataStructure::FEM::DRESP2Data::DRESP1Data;
    using SubStructDRESP2  = SG::DataStructure::FEM::DRESP2Data::DRESP2SubData;
    using SubStructDTABLE  = SG::DataStructure::FEM::DRESP2Data::DTABLEData;
    using SubStructDVCREL1 = SG::DataStructure::FEM::DRESP2Data::DVCREL1Data;
    using SubStructDVCREL2 = SG::DataStructure::FEM::DRESP2Data::DVCREL2Data;
    using SubStructDVMREL1 = SG::DataStructure::FEM::DRESP2Data::DVMREL1Data;
    using SubStructDVMREL2 = SG::DataStructure::FEM::DRESP2Data::DVMREL2Data;
    using SubStructDVPREL1 = SG::DataStructure::FEM::DRESP2Data::DVPREL1Data;
    using SubStructDVPREL2 = SG::DataStructure::FEM::DRESP2Data::DVPREL2Data;

    using NasMainStruct = SG::DBManager::HDF5::DRESP2::IDENTITY;

  public:
    explicit DRESP2Repository (HDF5Database& database);

    enum DATASET : std::uint8_t
    {
        MainDataset,
        IndexDataset,
        SubDataset_DESVAR,
        SubDataset_DNODE,
        SubDataset_DRESP1,
        SubDataset_DRESP2,
        SubDataset_DTABLE,
        SubDataset_DVCREL1,
        SubDataset_DVCREL2,
        SubDataset_DVMREL1,
        SubDataset_DVMREL2,
        SubDataset_DVPREL1,
        SubDataset_DVPREL2
    };

    /**
     * @brief   : 初始化表数据
     * @author  : wuxingchuang
     */
    void InitialDatasetInfo () override;

    /**
     * @brief   : 将缓存数据转储到文件内
     */
    void DumpWriteCahce () override;

    /**
     * @brief   : 加载文件数据至缓存
     * @param   : loadIndex
     */
    std::vector<DataType>* LoadReadCache (std::size_t loadIndex) override;

    /**
     * @brief   : 对数据进行排序
     */
    void SortData () override;

    /**
     * @brief   : 获取所有数据
     */
    std::vector<MainStruct> GetAllData () override;

    /**
     * @brief   : 将自己的数据转存为nastran的数据结构
     */
    void OwnToNastran (std::vector<MainStruct>&            own,
                       std::vector<NasMainStruct>& _OUT    IDENTITYData,
                       std::vector<SubStructDESVAR>& _OUT  subDataDESVAR,
                       std::vector<SubStructDNODE>& _OUT   subDataDNODE,
                       std::vector<SubStructDRESP1>& _OUT  subDataDRESP1,
                       std::vector<SubStructDRESP2>& _OUT  subDataDRESP2,
                       std::vector<SubStructDTABLE>& _OUT  subDataDTABLE,
                       std::vector<SubStructDVCREL1>& _OUT subDataDVCREL1,
                       std::vector<SubStructDVCREL2>& _OUT subDataDVCREL2,
                       std::vector<SubStructDVMREL1>& _OUT subDataDVMREL1,
                       std::vector<SubStructDVMREL2>& _OUT subDataDVMREL2,
                       std::vector<SubStructDVPREL1>& _OUT subDataDVPREL1,
                       std::vector<SubStructDVPREL2>& _OUT subDataDVPREL2,

                       bool isSort = false);

    /**
     * @brief   : 将NASTRAN的数据转存为自己的数据结构
     * @param   : IDENTITYData
     * @param   : subData
     * @param   : own
     */
    void NastranToOwn (std::vector<NasMainStruct>&    IDENTITYData,
                       std::vector<SubStructDESVAR>&  subDataDESVAR,
                       std::vector<SubStructDNODE>&   subDataDNODE,
                       std::vector<SubStructDRESP1>&  subDataDRESP1,
                       std::vector<SubStructDRESP2>&  subDataDRESP2,
                       std::vector<SubStructDTABLE>&  subDataDTABLE,
                       std::vector<SubStructDVCREL1>& subDataDVCREL1,
                       std::vector<SubStructDVCREL2>& subDataDVCREL2,
                       std::vector<SubStructDVMREL1>& subDataDVMREL1,
                       std::vector<SubStructDVMREL2>& subDataDVMREL2,
                       std::vector<SubStructDVPREL1>& subDataDVPREL1,
                       std::vector<SubStructDVPREL2>& subDataDVPREL2,
                       std::vector<MainStruct>& _OUT  own);
};
SG_DBMANAGER_HDF5_NAMESPACE_CLOSE
